package com.zzty.servlet;

import com.zzty.common.Log;
import com.zzty.dao.ShareDao;
import com.zzty.daoimpl.ShareDaoImpl;
import com.zzty.entity.Question;
import com.zzty.entity.Share;
import com.zzty.service.ShareServiceDao;
import com.zzty.serviceimpl.ShareServiceImpl;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@WebServlet(name="ShareServlet",urlPatterns="/ShareServlet")
public class ShareServlet extends HttpServlet {
    private static final Logger logger = LoggerFactory.getLogger(Log.class);
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if ("insertTo".equals(method)) {
            try {
                insertTo(request, response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method");
        if ("searchContent".equals(method)) {
            searchContent(request, response);
        }
        if("searchToday".equals(method)){
            searchToday(request,response);
        }
    }
    protected void searchToday(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String date = request.getParameter("date");
        ShareDao sd = new ShareDaoImpl();

        String content = sd.searchToday(date);
        String json=null;
        ObjectMapper mapper = new ObjectMapper();
        if(content==null){
            json = mapper.writeValueAsString("今日没有分享内容!!!!!");
        }
        else{
            json = mapper.writeValueAsString(content);
        }
        logger.info("json="+json);
        PrintWriter out= response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void searchContent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ShareDao sd = new ShareDaoImpl();
        List<Share>shares;
        String json=null;
        ObjectMapper mapper = new ObjectMapper();
        if(sd.getShare()!=null){
            shares = sd.getShare();
            json = mapper.writeValueAsString(shares);
        }
      logger.info("json="+json);
        PrintWriter out= response.getWriter();
        out.write(json);
        out.flush();
        out.close();
    }
    protected void insertTo(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        String json1=readJSONData(request);
        ObjectMapper mapper=new ObjectMapper();

        Share share_new=mapper.readValue(json1, Share.class);
        String content=share_new.getContent();
        String date = share_new.getDate();
        System.out.println("content : "+content);
        System.out.println("date : "+date);
        ShareServiceDao ssd = new ShareServiceImpl();
        String s=ssd.updateContent(content,date);
        String json=mapper.writeValueAsString(s);
        logger.info("json="+json);
        PrintWriter out=response.getWriter();
        out.write(json);
        out.flush();
        out.close();

    }

    private String readJSONData(HttpServletRequest request) {
        StringBuffer json=new StringBuffer();
        String lineString=null;
        try {
            BufferedReader reader=request.getReader();
            while ((lineString=reader.readLine()) != null) {
                json.append(lineString);
            }
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return json.toString();
    }
}